﻿using ChatTools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChatClient
{
    public partial class FormClient : Form
    {
        public FormClient()
        {
            InitializeComponent();
            this.Load += FormClient_Load;
        }

        public FormClient(int friendid, string nickname, string myname)
            : this()
        {
            _friendID = friendid;
            _nickname = nickname;
            _myname = myname;
        }

        public int _friendID;
        private string _nickname, _myname;
        public event EventHandler SendMsg;

        private void FormClient_Load(object sender, EventArgs e)
        {
            lblFriend.Text = _nickname;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Send();
        }

        public string GetMsg()
        {
            string msg = txtChat.Text;
            this.txtChat.Clear();
            return msg;
        }

        private void Send()
        {
            string msg1 = _myname + "：" + "\r\n";
            this.rtxtChat.AppendText(msg1);
            this.rtxtChat.Select(this.rtxtChat.Text.LastIndexOf(_myname), msg1.Length);
            this.rtxtChat.SelectionColor = Color.Gray;
            this.rtxtChat.SelectionFont = new Font("宋体", 9, FontStyle.Regular);
            this.rtxtChat.SelectionAlignment = HorizontalAlignment.Right;

            string msg2 = this.txtChat.Text.Trim() + "\r\n";
            this.rtxtChat.AppendText(msg2);
            this.rtxtChat.Select(this.rtxtChat.Text.LastIndexOf(this.txtChat.Text.Trim()), msg2.Length);
            this.rtxtChat.SelectionColor = Color.Black;
            this.rtxtChat.SelectionFont = new Font("微软雅黑", 12, FontStyle.Bold);
            this.rtxtChat.SelectionAlignment = HorizontalAlignment.Right;

            this.rtxtChat.ScrollToCaret();
            SendMsg?.Invoke(this, null);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData != Keys.Return)
                return base.ProcessDialogKey(keyData);
            this.Send();
            return true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.rtxtChat.Clear();
        }

        public void AppendMsg(string msg)
        {
            string msg1 = _nickname + "：" + "\r\n";
            this.rtxtChat.AppendText(msg1);
            this.rtxtChat.Select(this.rtxtChat.Text.LastIndexOf(_nickname), msg1.Length);
            this.rtxtChat.SelectionColor = Color.Gray;
            this.rtxtChat.SelectionFont = new Font("宋体", 9, FontStyle.Regular);
            this.rtxtChat.SelectionAlignment = HorizontalAlignment.Left;

            string msg2 = msg + "\r\n";
            this.rtxtChat.AppendText(msg2);
            this.rtxtChat.Select(this.rtxtChat.Text.LastIndexOf(msg), msg2.Length);
            this.rtxtChat.SelectionColor = Color.Black;
            this.rtxtChat.SelectionFont = new Font("微软雅黑", 12, FontStyle.Bold);
            this.rtxtChat.SelectionAlignment = HorizontalAlignment.Left;

            this.rtxtChat.ScrollToCaret();
        }
    }
}
